package com.ouyeel.wlxer.model.imp;

import android.app.Activity;

import com.ouyeel.wlxer.activity.AppStart;
import com.ouyeel.wlxer.activity.LoginActivity;
import com.ouyeel.wlxer.api.RetrofitClicent;
import com.ouyeel.wlxer.base.BaseActivity;
import com.ouyeel.wlxer.base.BaseObserver;
import com.ouyeel.wlxer.bean.LoginOut;
import com.ouyeel.wlxer.bean.VersionIn;
import com.ouyeel.wlxer.bean.VersionOut;
import com.ouyeel.wlxer.model.LoginModel;

import io.reactivex.Observable;
import okhttp3.RequestBody;

/**
 * Created by rankaifeng on 2017/10/26.
 */

public class LoginModelImp implements LoginModel {
    RetrofitClicent retrofitClicent = RetrofitClicent.getInstanse();

    @Override
    public void getAppVersion(final Activity activity, VersionIn versionIn) {
        Observable<VersionOut> appVersion =
                retrofitClicent.getRetrofit(VersionOut.class).getAppVersion(versionIn);
        retrofitClicent.toSubscribe(appVersion, new BaseObserver<VersionOut>(activity) {
            @Override
            public void success(VersionOut versionOut) {
                if (versionOut.getFlag() == 1) {
                    ((AppStart) activity).requestVersionSuccess(versionOut);
                } else {
                    ((AppStart) activity).showTost(versionOut.getMsg());
                    ((AppStart) activity).changeActivity();
                }
            }
        });

    }

    @Override
    public void userLogin(final Activity activity, RequestBody body) {
        Observable<LoginOut> loginObser = retrofitClicent.getRetrofit(LoginOut.class).login(body);
        retrofitClicent.toSubscribe(loginObser, new BaseObserver<LoginOut>(activity) {
            @Override
            public void success(LoginOut loginOut) {
                if (loginOut.getFlag() == 1) {
                    if (activity instanceof LoginActivity) {
                        ((LoginActivity) activity).loginSuccess(loginOut);
                    }
                } else {
                    ((BaseActivity) activity).showTost(loginOut.getMsg());
                }
            }
        });
    }
}
